<template>
  <section class="todoapp">
    <TodoHeader @add="addFn"></TodoHeader>
    <TodoMain
      :list="list"
      :type="type"
      @del="delFn"
      @change="changeFn"
      @revamp="revampFn"
      @checkAll="checkAllFn"
    ></TodoMain>
    <TodoFooter
      v-if="list.length > 0"
      :list="list"
      @clear="clearFn"
      :type="type"
      @filter="filterFn"
    ></TodoFooter>
  </section>
</template>

<script>
import TodoHeader from './components/TodoHeader'
import TodoMain from './components/TodoMain'
import TodoFooter from './components/TodoFooter'
export default {
  components: {
    TodoHeader,
    TodoMain,
    TodoFooter,
  },
  data() {
    return {
      list: [
        {
          id: 1,
          name: '吃饭',
          isDone: false,
        },
        {
          id: 2,
          name: '睡觉',
          isDone: true,
        },
        {
          id: 3,
          name: '学习',
          isDone: false,
        },
      ],
      type: 'all',
    }
  },
  methods: {
    delFn(id) {
      // console.log('接收到了', id)
      this.list = this.list.filter(item => item.id !== id)
    },
    changeFn(id) {
      // console.log('接收到了', id)
      const obj = this.list.find(item => item.id === id)
      obj.isDone = !obj.isDone
    },
    addFn(name) {
      this.list.unshift({
        id: Date.now(),
        name,
        isDone: false,
      })
    },
    revampFn(name, id) {
      this.list.find(item => item.id === id).name = name
    },
    clearFn() {
      this.list = this.list.filter(item => !item.isDone)
    },
    filterFn(type) {
      this.type = type
    },
    checkAllFn(value) {
      this.list.forEach(item => (item.isDone = value))
    },
  },
  watch: {
    list: {
      deep: true,
      handler(value) {
        localStorage.setItem('list', JSON.stringify(value))
      },
    },
  },
  // type在footer组件情况
  // computed: {
  //   showList() {
  //     if (this.type === 'active') {
  //       return this.list.filter(item => !item.isDone)
  //     } else if (this.type === 'completed') {
  //       return this.list.filter(item => item.isDone)
  //     } else {
  //       return this.list
  //     }
  //   },
  // },
}
</script>

<style></style>
